home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / doorinfo.zip / TLCFDEMO.C < prev    next >
C/C++ Source or Header  |  1997-05-14  |  6KB  |  237 lines

  1. /*
  2. ** This is a small demonstration of a multi-player door for Falken.
  3. ** This program is loaded by the 'loadtlcf.exe' door.  See the source
  4. ** code for that program as well.
  5. **
  6. ** This sample code implements a tiny teleconference under Falken.
  7. ** Everything you type is sent to the other players in the door.
  8. ** If you type '.exit' then you leave.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "doorutil.h"
  15.  
  16. int intlcf[65];            /* 65 is the maximum number of lines for
  17.                  * Falken */
  18.  
  19. struct msg1 tlcfm1;        /* structure used to receive messages from
  20.                  * Falken */
  21.  
  22. char welcome_msg[]=
  23. "This is a teleconference demo.\r"
  24. "Whatever you type is sent to other people in the door.\r"
  25. "Type .exit to leave.\r";
  26.  
  27. void tlcf_exit(int who)
  28. {
  29.     int j;
  30.     intlcf[who] = 0;        /* he is no longer here. */
  31. /*
  32. ** tell the gang that he is gone!
  33. */
  34.     for (j = 0; j < numlines; j++)
  35.     {
  36.     if (intlcf[j])
  37.     {
  38.         qnprintf(j, "%s has left TLCFDEMO.\r",
  39.              acct[who].acctname);
  40.     }
  41.     }
  42. /*
  43. ** last thing to do - get his loadtlcf door running again.
  44. ** the next instruction it executes should be an exit(), to
  45. ** get him back to the Falken menu.
  46. */
  47.  
  48.     wakeup(user[who].u_doortcb);
  49. }
  50.  
  51. void p_tlcf(int who)
  52. {
  53.     int j;
  54.     if (strcmpi(".exit", tlcfm1.text) == 0)
  55.     {
  56.     tlcf_exit(who);
  57.     }
  58.     else
  59.     {
  60. /*
  61. ** tell the gang what he typed!
  62. */
  63.     for (j = 0; j < numlines; j++)
  64.     {
  65.         if (intlcf[j])
  66.         {
  67.         qnprintf(j, "From %s : %s\r",
  68.              acct[who].acctname, tlcfm1.text);
  69.         }
  70.     }
  71.     }
  72. }
  73.  
  74. main()
  75. {
  76.     int j, k, n, m;
  77.     int playercount;
  78.  
  79. /*
  80. ** Basic initialization for a Falken door.
  81. */
  82.  
  83.     init();
  84.  
  85. /*
  86. ** Special initialization for a multi-player door.
  87. **
  88. ** This single task will handle multiple players.  It will be loaded
  89. ** by the 'loadtlcf' door.  The 'loadtlcf' door will have checked to
  90. ** see if a copy of this program was active already, but there is
  91. ** still a possibility that 2 copies can be loaded, if the 'loadtlcf'
  92. ** doors are run very close together.
  93. **
  94. ** So... use the hog() function to prevent any other tasks from
  95. ** running while we check to see if another copy is already active.
  96. */
  97.  
  98.     hog();            /* we need to stop multitasking momentarily */
  99.  
  100.     if (check_tcb_names("tlcfdemo") == 1)
  101.     {
  102. /*
  103. ** if check_tcb_names() returns a 1, a task with this name exists.
  104. ** the nohog() is not really necessary, but it is good practice to
  105. ** make sure that a nohog() is called after every hog()
  106. */
  107.     nohog();
  108.     exit(0);
  109.     }
  110.  
  111. /*
  112. ** If we get here, by falling through the check_tcb_names() check,
  113. ** then there are no active tasks with the name "tlcfdemo".
  114. ** Set our name to that value, so that any other tlcfdemo programs
  115. ** that start up will not find it, and exit gracefully.
  116. */
  117.  
  118.     set_tcb_name("tlcfdemo");
  119.  
  120. /*
  121. ** It is now safe to let other tasks execute again.
  122. */
  123.  
  124.     nohog();
  125.  
  126. /*
  127. ** the 'intlcf' array of flags is used internally to mark the lines
  128. ** that we know are already in the game.
  129. */
  130.  
  131.     for (j = 0; j < 65; j++)
  132.     intlcf[j] = 0;        /* nobody here yet */
  133.  
  134. /*
  135. ** Now a forever loop.
  136. **
  137. ** We recognize a player has entered this program when the following
  138. ** conditions are true :
  139. **    - the u_stat variable in his/her user structure is 6 (in doors).
  140. **    - the doors_id is set the string 'tlcfdemo'.
  141. **
  142. ** We must look at each line on the system, and if the above criteria
  143. ** are met, we must check the message queue for that line to see if
  144. ** there is text or a termination event for us to handle.
  145. **
  146. ** The playercount variable is used to find out how many users are
  147. ** actually in the tlcfdemo door.  It simply counts how many matches
  148. ** of the above criteria we get on each pass through the active lines.
  149. */
  150.  
  151.     for (;;)
  152.     {
  153.     playercount = 0;    /* set to 0 */
  154.  
  155. /*
  156. ** Many of the doorutil routines use the global 'who' variable to
  157. ** decide which line to send an event for.  We will use this global
  158. ** variable also.
  159. */
  160.  
  161.     for (who = 0; who < numlines; who++)
  162.     {
  163.         if ((user[who].u_stat == 6) &&
  164.         (strcmpi(user[who].doors_id, "tlcfdemo") == 0))
  165.         {
  166.         playercount++;    /* found a player! hallelujah */
  167. /*
  168. ** We have found a user who meets the criteria for being in tlcfdemo.
  169. ** If the 'intlcf' flag is not set, then this is the FIRST TIME that he
  170. ** has met this critereia, so send him a nice welcome message.
  171. */
  172.  
  173.         if (intlcf[who] == 0)    /* a new player! */
  174.         {
  175.             send(welcome_msg);
  176.             intlcf[who] = 1;    /* no longer new */
  177.         }
  178. /*
  179. ** Let's see if there are any events waiting on the input queue for
  180. ** this user.  It is our responsibility to handle these messages,
  181. ** since he has joined our game!
  182. */
  183.         if (testmsg(user[who].door_inq) > 0)
  184.         {
  185. /*
  186. ** testmsg() returns a positive number if a message is waiting.
  187. ** recvmsg() reads the first message from the queue.
  188. */
  189.             recvmsg(user[who].door_inq, &tlcfm1, 1100);
  190.             if (tlcfm1.type == 8)
  191.             {
  192. /*
  193. ** message type 8 is an exit code from Falken.  The guy probably
  194. ** dropped carrier
  195. */
  196.             tlcf_exit(who);
  197.             }
  198.             else if (tlcfm1.type == 1)
  199.             {
  200. /*
  201. ** message type 1 is a normal text string entered by the user.
  202. */
  203.             p_tlcf(who);
  204.             }
  205.         }
  206.         }
  207. /*
  208. ** the else referred to here is 'else the user is not in tlcfdemo'.
  209. ** If the guy is not in our door, but the intlcf flag says he is,
  210. ** then he has somehow exited the door without our knowledge.
  211. ** handle the cleanup involved with the guy leaving.
  212. */
  213.  
  214.         else if (intlcf[who] != 0)
  215.         {
  216.         tlcf_exit(who);    /* we think he's online, but he's not */
  217.         }
  218.     }
  219. /*
  220. ** If playercount is 0, then we went through all the lines without finding
  221. ** a user in our game.  Time to leave.
  222. */
  223.     if (playercount == 0)
  224.     {
  225.         (0);
  226.     }
  227. /*
  228. ** We handled all the lines, and there is still more to do.
  229. ** the relinq() call is used to allow other doors to run.
  230. ** It is not necessary, but it does keep us from looping while
  231. ** we look at empty queues, which is a great waste of time.
  232. */
  233.  
  234.     relinq();
  235.     }                /* end for(;;) */
  236. }
  237.